﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using System.Data;
using System.Data.SqlClient;
public partial class User_Shares_Notification : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Lblstatus.Text = "";
        if (!IsPostBack)
        {
            Button1.Attributes.Add("onclick", "return confirm('هشدار : آيا از حذف پیغام های انتخابی مطمئن هستيد؟');");
   
            SubSonic.Query q = DataAccess.TblNotification.CreateQuery();
            q.QueryType = SubSonic.QueryType.Update;
            q.AddWhere(DataAccess.TblNotification.Columns.UserId, Membership.GetUser().ProviderUserKey);
            q.AddUpdateSetting(DataAccess.TblNotification.Columns.IsRead, true);
            q.Execute();
            SubSonic.Query q1 = DataAccess.TblNotification.CreateQuery();
            q1.QueryType = SubSonic.QueryType.Select;
            q1.AddWhere(DataAccess.TblNotification.Columns.UserId, Membership.GetUser().ProviderUserKey);
            q1.OrderBy = SubSonic.OrderBy.Desc("Date");
            GridViewNotific.DataSource = q1.ExecuteReader();
            GridViewNotific.DataBind();
            if (GridViewNotific.Rows.Count <=0)
                Button1.Visible = false;
            else
                Button1.Visible = true;
        }
    }
    protected void GridViewNotific_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Business.Calender cal = new Business.Calender();
            e.Row.Cells[2].Text = PersianDateControls.Convertor.ToPersianDate(Convert.ToDateTime(e.Row.Cells[2].Text)) + " " + Convert.ToDateTime(e.Row.Cells[2].Text).ToShortTimeString().Replace("AM", "صبح").Replace("PM", "عصر");
            
        }
    }
    protected void CheckBoxItemSelect_CheckedChanged(object sender, EventArgs e)
    {
List<Guid> list = new List<Guid>();
if (Session["notificIds"] != null)
        {
            list = Session["notificIds"] as List<Guid>;
        }

        CheckBox checkbox = (CheckBox)sender;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;
        if (checkbox.Checked == true)
        {
            row.BackColor = System.Drawing.Color.Red;
            list.Add(new Guid(GridViewNotific.DataKeys[row.RowIndex].Value.ToString()));
        }
        if (checkbox.Checked == false)
        {
            list.Remove(new Guid(GridViewNotific.DataKeys[row.RowIndex].Value.ToString()));
            if (row.RowIndex % 2 == 0)
            {
                row.BackColor = System.Drawing.Color.AliceBlue;
            }
            else
            {
                row.BackColor = System.Drawing.Color.PowderBlue;
            }
        }
        Session["notificIds"] = list;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<Guid> list = new List<Guid>();
          Guid nid=new Guid();
        if (Session["notificIds"] != null)
        {
            list = Session["notificIds"] as List<Guid>;
            for (int i = 0; i <= list.Count - 1; i++)
            {
                nid = new Guid(list[i].ToString());
                DataAccess.TblNotification.Delete(nid);
                SubSonic.Query q1 = DataAccess.TblNotification.CreateQuery();
                q1.QueryType = SubSonic.QueryType.Select;
                q1.AddWhere(DataAccess.TblNotification.Columns.UserId, Membership.GetUser().ProviderUserKey);
                q1.OrderBy = SubSonic.OrderBy.Desc("Date");
                
                GridViewNotific.DataSource =  q1.ExecuteReader();
                GridViewNotific.DataBind();
                if (GridViewNotific.Rows.Count <= 0)
                    Button1.Visible = false;
                else
                    Button1.Visible = true;
            }
        }
        else

        {
            Lblstatus.Text = " <script>alert('.پیغامی انتخاب نگردیده است ')</script>";
     
        }
    }
}